<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleSync" preIcon="ant-design:plus-outlined"> 全量同步</a-button>
          <a-dropdown v-if="selectedRowKeys.length > 0">
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1" @click="batchHandleDelete">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    删除
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button>批量操作
                <Icon icon="mdi:chevron-down"></Icon>
              </a-button>
        </a-dropdown>
      </template>

     <template #infoSlot="{ record }">
       <div class="infoBox">
       <img class="image" :src="baseApiUrl+'/'+VITE_GLOB_BUCKET_NAME+'/'+record.personPhoto" v-if="record.personPhoto" />
       <img class="image" v-else src="https://ecard.dglemin.com.cn:55005/images/default_avatar.png" />
       </div>
     </template>



       <!--操作栏-->
      <template #action="{ record }">
        <TableAction  :dropDownActions="getDropDownAction(record)"/>
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <TracePersonBoxModal @register="registerModal" @success="handleSuccess"></TracePersonBoxModal>
  </div>
</template>

<script lang="ts" name="trace-tracePersonBox" setup>
  import {ref, reactive, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import TracePersonBoxModal from './components/TracePersonBoxModal.vue'
  import {columns, searchFormSchema, superQuerySchema} from './TracePersonBox.data';
  import {list,saveOrUpdate, deleteOne, batchDelete, getImportUrl,getExportUrl} from './TracePersonBox.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useUserStore } from '/@/store/modules/user';
  import {getAppEnvConfig} from "@/utils/env";
  import {useGlobSetting} from "@/hooks/setting";
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  const globSetting = useGlobSetting();
  const baseApiUrl = globSetting.domainUrl;
  const {VITE_GLOB_BUCKET_NAME} = getAppEnvConfig();
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: 'trace_person_box',
           api: list,
           columns,
           canResize:false,

           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      },
       exportConfig: {
            name:"trace_person_box",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext



  async function handleSync(){
    let res = await saveOrUpdate({},false);
    handleSuccess();
  }

   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
         }
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
     {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
             placement: 'topLeft',
           }
         }
       ]
   }


</script>

<style  lang="less"  scoped>
  .infoBox {
   .image {
    width: 50px;
    height: 50px;
    margin-right: 10px;
    border: 1px solid #f5f5f5;
  }
  }
</style>
